package org.pentaho.di.core.util;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @Description java8 Stream工具类
 * @ClassName StreamUtil
 * @Author tanp
 * @Date 2020/3/28 16:31
 * @Version 1.0
 */
public class StreamUtil {

	/**
	 * 根据属性去重
	 *
	 * @param keyExtractor
	 * @param <T>
	 * @return
	 */
	public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
		Map<Object, Boolean> seen = new ConcurrentHashMap<>();
		return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
	}
}
